<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>管理用户</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>

<style>

    .msg {
        color: red;
        text-align: center;
        margin-bottom: 15px;
        font-weight: bold;
    }
</style>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">图书馆管理系统 - 管理用户</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
                aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav">
                <li class="nav-item">
                    <a class="nav-link" href="adminDashboardServlet">返回控制面板</a>
                </li>
            </ul>
            <ul class="navbar-nav ms-auto">
                <li class="nav-item">
                    <a class="nav-link" href="logout.jsp">注销登录</a>
                </li>
                <li class="nav-item">
                    <c:if test="${not empty sessionScope.user}">
                        <span class="nav-link">当前登录用户：${sessionScope.user.username}</span>
                    </c:if>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container mt-5">
    <c:if test="${msg!= null}">
        <p class="msg">${msg}</p>
    </c:if>
    <h2>用户管理</h2>
    <form action="userServlet" method="get" class="mb-3">
        <div class="input-group">
            <input type="hidden" name="path" value="getUsersByName">
            <input type="text" class="form-control" placeholder="输入用户名进行搜索" name="name">
            <button class="btn btn-primary" type="submit">搜索</button>
            <a href="userServlet?path=getById" class="btn btn-success">添加用户</a>
        </div>
    </form>
    <table class="table table-striped">
        <thead>
        <tr>
            <th>用户ID</th>
            <th>用户名</th>
            <th>邮箱</th>
            <th>类型</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="user" items="${users}">
            <tr>
                <td>${user.id}</td>
                <td>${user.username}</td>
                <td>${user.email}</td>
                <td>${user.type==1?"管理员":"用户"}</td>
                <td>
                    <a href="userServlet?path=getById&id=${user.id}" class="btn btn-warning btn-sm">编辑</a>
                    <a href="#" onclick="deleteUser('${user.id}')" class="btn btn-danger btn-sm">删除</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>

</div>

<!-- 引入Bootstrap JS（用于导航栏折叠等交互功能） -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>

</html>
<script>
    function deleteUser(id) {
        if (id === ${sessionScope.user.id}) {
            alert("不能删除当前登录用户！");
            return false
        }
        // 确认删除
        if (confirm("确定要删除该用户吗？")) {
            window.location.href = 'userServlet?path=delete&id=' + id;
        }
    }
</script>